//
//  ShopManagerVC.m
//  LazyCat
//
//  Created by linhui on 15/12/12.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "ShopManagerVC.h"
#import "PeiSongViewController.h"
#import "OpenTimeViewController.h"
#import "ShopBuildViewController.h"
#import "ShopManagerVCCell.h"
#import "AFNetworkHandler.h"

@interface ShopManagerVC ()<UITableViewDataSource,UITableViewDelegate,UIPickerViewDataSource,UIPickerViewDelegate>
{
    UITableView *_tableView;
    NSArray *_dataArray;
    NSMutableArray *_dataArray1;
    NSMutableArray *_dataArray2;
    UIPickerView *_pickerView;
    UIView *_timeView;
    NSString *_timeLabel2;//营业时间
    UIView *popView;
    NSString *start;
    NSString *end;
}
@end

@implementation ShopManagerVC


-(void)viewWillAppear:(BOOL)animated
{
    if ([AppData share].openingstart.length < 1) {
        _timeLabel2 = @"";
    }else{
        _timeLabel2 = [NSString stringWithFormat:@"%@~%@",[AppData share].openingstart,[AppData share].openingend];
    }

}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self createUI];
    [self createPickerView];
}
- (void)createUI {
    self.view.backgroundColor = BackColor;
    self.automaticallyAdjustsScrollViewInsets=NO;
    [self initNav:@"店铺管理" doBack:YES];
    _dataArray=[[NSMutableArray alloc]init];
    _dataArray = @[@"店铺设置",@"营业时间",@"配送费"];
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStyleGrouped];
    _tableView.dataSource=self;
    _tableView.delegate=self;
    _tableView.scrollEnabled = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    _dataArray2 = [[NSMutableArray alloc]init];
    _dataArray1 = [[NSMutableArray alloc]init];
    for (int i = 0; i<24; i++) {
        if (i < 10) {
            [_dataArray1 addObject:[NSString stringWithFormat:@"0%d:00",i]];
        }else{
            [_dataArray1 addObject:[NSString stringWithFormat:@"%d:00",i]];
        }
    }
    for (int i = 1; i<25; i++) {
        if (i < 10) {
            [_dataArray2 addObject:[NSString stringWithFormat:@"0%d:00",i]];
        }else{
            [_dataArray2 addObject:[NSString stringWithFormat:@"%d:00",i]];
        }
    }
}
- (void)createPickerView {
    // 蒙版
    UIWindow *keyWindow=[UIApplication sharedApplication].keyWindow;
    popView=[[UIView alloc]initWithFrame:keyWindow.bounds];
    popView.backgroundColor=SetColor(0, 0, 0, .4);
    popView.alpha=0;
    [keyWindow addSubview: popView];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hidentPopView)];
    [ popView addGestureRecognizer:tap];
    //时间view
    _timeView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth, 200)];
    _timeView.backgroundColor = [UIColor whiteColor];
    [popView addSubview:_timeView];
    //时间view上面的东西
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 45)];
    backView.backgroundColor = BackColor;
    [_timeView addSubview:backView];
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(0, 0, 60, 45);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:MainColor forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(hidentPopViewqq) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:cancelBtn];
    UIButton *qdBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    qdBtn.frame = CGRectMake(ScreenWidth-60, 0, 60, 45);
    [qdBtn setTitle:@"确定" forState:UIControlStateNormal];
    [qdBtn setTitleColor:MainColor forState:UIControlStateNormal];
    [qdBtn addTarget:self action:@selector(tishi) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:qdBtn];
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(60, 0, ScreenWidth-120, 45)];
    label1.text = @"上班时间              下班时间";
    label1.font = [UIFont systemFontOfSize:14];
    label1.textAlignment = NSTextAlignmentCenter;
    label1.textColor = [UIColor lightGrayColor];
    [backView addSubview:label1];
    _pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, ScreenWidth, 160)];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    _pickerView.showsSelectionIndicator = YES;
    [_timeView addSubview:_pickerView];
    NSString *kaishi = [[AppData share].openingstart substringToIndex:2];
    NSLog(@"%@",kaishi);
    NSString *jieshu = [[AppData share].openingend substringToIndex:2];
    NSLog(@"%@",jieshu);
    NSInteger ks = [kaishi integerValue];
    NSInteger js = [jieshu integerValue];
    [_pickerView selectRow:ks inComponent:0 animated:NO];
    [_pickerView selectRow:js-1 inComponent:1 animated:NO];
    CGPoint center = _timeView.center;
    center.y = ScreenHeight-100;
}
#pragma mark -- UIPickerView的协议方法
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return _dataArray1.count;
    }else {
        return _dataArray2.count;
    }
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component==0) {
        return _dataArray1[row];
    }else{
        return _dataArray2[row];
    }
}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 35;
}
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component == 0) {
        return ScreenWidth/2+20;
    }else {
        return ScreenWidth/2-20;
    }
}
#pragma mark - 设置piker显示内容
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *myView = nil;
    if (component == 0) {
        myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, 30)];
        myView.textAlignment = NSTextAlignmentCenter;
        myView.text = [_dataArray1 objectAtIndex:row];
        myView.font = [UIFont systemFontOfSize:14];         //用label来设置字体大小
        myView.backgroundColor = [UIColor clearColor];
    }else {
        myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 180, 30)];
        myView.text = [_dataArray2 objectAtIndex:row];
        myView.textAlignment = NSTextAlignmentCenter;
        myView.font = [UIFont systemFontOfSize:14];
        myView.backgroundColor = [UIColor clearColor];
    }
    return myView;
}
#pragma mark - 确认点击方法
- (void)tishi {
    NSInteger num0 = [_pickerView selectedRowInComponent:0];
    NSInteger num1 = [_pickerView selectedRowInComponent:1];
    NSLog(@"%d===%d",num0,num1);
    if (num0 <= num1) {
        start = [_dataArray1 objectAtIndex:[_pickerView selectedRowInComponent:0]];
        end = [_dataArray2 objectAtIndex:[_pickerView selectedRowInComponent:1]];
        CGPoint center = _timeView.center;
        center.y = ScreenHeight+100;
        
        [UIView animateWithDuration:0.2 animations:^{
            //设置要被动画的那个视图
            //在动画结束时，它的属性处于什么状态
            _timeView.center = center;
            popView.alpha = 0;
        }];
        NSString *url = [NSString stringWithFormat:upTimeUrl,[AppData share].username,start,end];
        [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
            NSDictionary *dic = result;
            if ([dic[@"r"] isEqualToString:@"T"]) {
                //[[MessShowView share] showWithMess:@"修改成功" login:YES];
                
                _timeLabel2 = [NSString stringWithFormat:@"%@~%@",start,end];
                [AppData share].openingstart = start;
                [AppData share].openingend = end;
                [_tableView reloadData];
                
            }
        }];
        [_tableView reloadData];
    }else {
        CGPoint center = _timeView.center;
        center.y = ScreenHeight+100;
        
        [UIView animateWithDuration:0.2 animations:^{
            //设置要被动画的那个视图
            //在动画结束时，它的属性处于什么状态
            _timeView.center = center;
            popView.alpha = 0;
        }];
        [[MessShowView share] showWithMess:@"下班时间必须大于上班时间" login:NO];
        
        return;
    }
}
- (void)hidentPopViewqq {
    CGPoint center = _timeView.center;
    center.y = ScreenHeight+100;
    [UIView animateWithDuration:0.2 animations:^{
        //设置要被动画的那个视图
        //在动画结束时，它的属性处于什么状态
        _timeView.center = center;
         popView.alpha = 0;
    }];
}
#pragma mark - pick滚动代理方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        start = _dataArray1[row];
    }else {
        end = _dataArray2[row];
    }
}
#pragma mark -- tableView代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ShopManagerVCCell *cell = [tableView dequeueReusableCellWithIdentifier:@"shopvccell"];
    if(cell==nil){
        cell = [[NSBundle mainBundle]loadNibNamed:@"ShopManagerVCCell" owner:nil options:nil][0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 1) {
        if ([_timeLabel2 isEqualToString:@""]) {
            cell.timeLabel.placeholder = @"店铺营业时间";
        }else{
            cell.timeLabel.placeholder = _timeLabel2;
        }
        cell.timeLabel.hidden = NO;
        cell.timeLabel.enabled = NO;
    }
    cell.titleLabel.text = _dataArray[indexPath.row];
    cell.iconImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"shopManager%ld",(long)indexPath.row]];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) { // 店铺设置
        ShopBuildViewController *shopBuild = [[ShopBuildViewController alloc]init];
        [self.navigationController pushViewController:shopBuild animated:YES];
    }else if (indexPath.row == 1){ // 营业时间
        [self mengBan];
    }else if (indexPath.row ==2){ // 配送费
        PeiSongViewController *peiSong = [[PeiSongViewController alloc] init];
        peiSong.titleStr = _dataArray[indexPath.row];
        [self.navigationController pushViewController:peiSong animated:YES];
    }
}
-(void)mengBan {
    [UIView animateWithDuration:0.2 animations:^{
         popView.alpha=1;
         _timeView.frame = CGRectMake(0, ScreenHeight - 200, ScreenWidth, 200);
    }];
}
-(void)hidentPopView {
    CGPoint center = _timeView.center;
    center.y = ScreenHeight+100;
    [UIView animateWithDuration:0.2 animations:^{
        //设置要被动画的那个视图
        //在动画结束时，它的属性处于什么状态
        _timeView.center = center;
        popView.alpha = 0;
    }];
}
@end
